package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.PermissionDto;
import com.zw.stock_sell_storage.dto.RoleDto;
import com.zw.stock_sell_storage.dto.UserDto;
import com.zw.stock_sell_storage.dto.ex.UserDtoEx;
import com.zw.stock_sell_storage.mapper.PermissionMapper;
import com.zw.stock_sell_storage.mapper.RoleMapper;
import com.zw.stock_sell_storage.mapper.UserMapper;
import com.zw.stock_sell_storage.service.IPermissionService;
import com.zw.stock_sell_storage.service.IRoleService;
import com.zw.stock_sell_storage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDto> implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    IRoleService iRoleService;
    @Autowired
    IPermissionService iPermissionService;

    @Override
    public UserDtoEx queryUser(UserDto userDto) {
        UserDtoEx userDtoEx =null;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",userDto.getUserName());
        queryWrapper.eq("user_password",userDto.getUserPassword());
        UserDto userResult = userMapper.selectOne(queryWrapper);
        if (userResult!=null){
            List<RoleDto> roleList = iRoleService.queryRoleByUserId(userResult.getUserId());
            List<PermissionDto> permissionList = iPermissionService.queryPermissionByUserId(roleList);
            userDtoEx=new UserDtoEx();
            userDtoEx.setUserId(userResult.getUserId());
            userDtoEx.setUserName(userResult.getUserName());
            userDtoEx.setUserPassword(userResult.getUserPassword());
            userDtoEx.setUserRealName(userResult.getUserRealName());
            userDtoEx.setRoleList(roleList);;
            userDtoEx.setPermissionList(permissionList);
        }
        return userDtoEx;
    }

    @Override
    public Page<UserDto> queryUserList(MyPageDto<UserDto> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getUserName()!=null&&!"".equals(myPageDto.getData().getUserName())){
            queryWrapper.like("user_name",myPageDto.getData().getUserName());
        }
        return userMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    @Transactional
    public boolean addUser(UserDtoEx userDtoEx) {
        userMapper.insert(userDtoEx);
        userMapper.insertUserLinkRole(userDtoEx);
        return true;
    }

    @Override
    @Transactional
    public boolean rmvUser(int userId) {
        userMapper.deleteUserLinkRole(userId);
        userMapper.deleteById(userId);
        return true;
    }

    @Override
    public UserDtoEx queryUserById(int userId) {
        UserDto userDto = userMapper.selectById(userId);
        List<RoleDto> roleList = iRoleService.queryRoleByUserId(userId);
        List<PermissionDto> permissionList = iPermissionService.queryPermissionByUserId(roleList);
        UserDtoEx userDtoEx=new UserDtoEx();
        userDtoEx.setUserId(userDto.getUserId());
        userDtoEx.setUserName(userDto.getUserName());
        userDtoEx.setUserPassword(userDto.getUserPassword());
        userDtoEx.setUserRealName(userDto.getUserRealName());
        userDtoEx.setRoleList(roleList);;
        userDtoEx.setPermissionList(permissionList);
        return userDtoEx;
    }

    @Override
    @Transactional
    public boolean updateUser(UserDtoEx userDtoEx) {
        userMapper.deleteUserLinkRole(userDtoEx.getUserId());
        userMapper.insertUserLinkRole(userDtoEx);
        return true;
    }

    @Override
    public boolean changePassWord(UserDto userDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userDto.getUserId());
        userMapper.update(userDto,queryWrapper);
        return true;
    }


}
